﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using AircraftBattleServer;
using System.Media;

namespace AircraftBattleClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        static GameEngineServiceClient clt = new GameEngineServiceClient();
        private Thread backgroundThread;
        public string username;
        public User me;
        public int craftId;
        public Image myAircraftImage;
        public double myAngle;
        public bool alive;
        public bool gameEnd;
        DateTime lastShootTime = DateTime.Now;
        public MyPoint initPosition;
        private SoundPlayer sndStart = new SoundPlayer(Constants.SoundFolderRelativePath
                + "\\" + "Start.wav");
        // lock for different movement keys
        public int up = 0, down = 0, left = 0, right = 0;

        // maybe we could use x and y to store this client's aircraft position, so we dont 
        // need to find its position everytime
        // by Pengfei

        public MainWindow(string username)
        {
            MapData mapdata = clt.GetMapData();

            InitializeComponent();
            this.username = username;
            clt.InitializePosition();
            this.alive = true;
            this.gameEnd = false;

            foreach (User player in mapdata.userList)
            {
                int id = clt.GetAircraftID(player.userName);
                MyPoint position = clt.GetPosition(id);

                Image img = BattleLib.CreateAircraftImage(this.cnvsMain, id, player.team, position.x, position.y);
                if (player.userName == username)
                {
                    sndStart.Play();
                    this.craftId = id;
                    this.me = player;
                    this.myAircraftImage = img;
                    this.myAngle = Constants.AircraftInitialAngle1;
                    this.initPosition = position;
                }
            }

            // Open a new thread for periodically poll 
            // the server for game data
            BackgroundWorker threadWork = new BackgroundWorker(this, username);
            Thread newThread =
                new Thread(new ThreadStart(threadWork.Run));
            this.backgroundThread = newThread;
            newThread.Start();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (!this.alive || gameEnd) return;

            if (Keyboard.IsKeyDown(Key.A))
            {
                left = 1;
            }
            if (Keyboard.IsKeyDown(Key.W))
            {
                up = 1;
            }
            if (Keyboard.IsKeyDown(Key.D))
            {
                right = 1;
            }
            if (Keyboard.IsKeyDown(Key.S))
            {
                down = 1;
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyUp(Key.A))
            {
                left = 0;
            }
            if (Keyboard.IsKeyUp(Key.W))
            {
                up = 0;
            }
            if (Keyboard.IsKeyUp(Key.D))
            {
                right = 0;
            }
            if (Keyboard.IsKeyUp(Key.S))
            {
                down = 0;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.backgroundThread.Abort();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!this.alive || gameEnd) return;

            if (Mouse.LeftButton == MouseButtonState.Pressed)
            {
                if (!BattleLib.MinimunShootingIntervalPassed(this.lastShootTime)) return;

                string mid = ClientUtility.GetMissileId(this.craftId);
                double x = Canvas.GetLeft(this.myAircraftImage) + Constants.AircraftImageWidth / 2;
                double y = Canvas.GetTop(this.myAircraftImage) + Constants.AircraftImageHeight / 2;
                x += Constants.AircraftImageWidth / 2 * Math.Cos(myAngle * Math.PI / 180);
                y += Constants.AircraftImageHeight / 2 * Math.Sin(myAngle * Math.PI / 180);
                Image missile = BattleLib.CreateMissileImage(this.cnvsMain, mid, x, y);
                clt.AddMissile(mid, x, y, myAngle);

                // reset last shooting time
                this.lastShootTime = DateTime.Now;
            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.alive || gameEnd) return;

            double angle = BattleLib.RotateImageToPoint(this.myAircraftImage,
                Mouse.GetPosition(this.cnvsMain), Constants.AircraftInitialAngle1);
            this.myAngle = angle;
            clt.SetAngle(craftId, angle);
        }

        public void ReturnBackToLounge()
        {
            GameLounge lounge = new GameLounge(this.username);
            lounge.Show();
            this.Close();
        }

    }


}
